#include "clickanimatecontrol.h"

#include <QTimer>
#include <QDebug>
#include <QApplication>
#include <QDesktopWidget>
#include <QScreen>

ClickAnimateControl::ClickAnimateControl(double time, int diameter, bool radiant, double opacity)
{
    m_showTime = time * 1000;

    m_globalMouse = new GlobalMouse();
    connect( m_globalMouse, SIGNAL( mousePressed( int, int ) ), this, SLOT( mousePressed( int, int ) ) );
    connect( m_globalMouse, SIGNAL( mousemove( int, int ) ), this, SLOT( mousemove( int, int ) ) );

    m_animateWindow = new AnimateWindow();
    setDiameterColor( diameter, Qt::red );
    setRadiant(radiant);
    setOpacity(opacity);
}

ClickAnimateControl::~ClickAnimateControl()
{
}

void ClickAnimateControl::pointerOnOff( bool value )
{
    if ( value == true )
        animateWindowOn();

    if ( value == false )
        animateWindowOff();
}

void ClickAnimateControl::setIsShowPath(bool value)
{
    m_isShowPath = value;
    m_animateWindow->setIsShowPath(value);
}

void ClickAnimateControl::setIsShowClick(bool value)
{
    m_isShowClick = value;
    m_animateWindow->setIsShowClick(value);
}

void ClickAnimateControl::animateWindowOn()
{
    m_globalMouse->setCursorOn();
    m_globalMouse->mousePressed();
}

void ClickAnimateControl::animateWindowOff()
{
    m_globalMouse->setCursorOff();
    if (m_animateWindow)
    {
        m_animateWindow->hide();
    }
}

void ClickAnimateControl::mousePressed( int x, int y )
{
    if (m_isShowClick)
    {
        auto screenNumber = QApplication::desktop()->screenNumber();
        QScreen *screen = QApplication::screens()[screenNumber];
        m_animateWindow->setIsPressed(true);
        m_animateWindow->setWindowFlags( Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::ToolTip );
        m_animateWindow->move( x/screen->devicePixelRatio() - m_animateWindow->width() / 2, y/screen->devicePixelRatio() - m_animateWindow->height() / 2 );
        m_animateWindow->show();
        QTimer::singleShot(m_showTime, this, SLOT( closeWindow() ) );
    }
}

void ClickAnimateControl::mousemove(int x, int y)
{
    if (m_isShowPath)
    {
        auto screenNumber = QApplication::desktop()->screenNumber();
        QScreen *screen = QApplication::screens()[screenNumber];
        m_animateWindow->setIsPressed(false);
        m_animateWindow->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);
        m_animateWindow->move(x/screen->devicePixelRatio() - m_animateWindow->width() / 2, y/screen->devicePixelRatio() - m_animateWindow->height() / 2);
        m_animateWindow->addTrailPoint(QPoint(x/screen->devicePixelRatio(), y/screen->devicePixelRatio()));
        m_animateWindow->show();
    }
}

void ClickAnimateControl::closeWindow()
{
    m_animateWindow->close();
}

void ClickAnimateControl::setDiameterColor( int diameter, QColor color)
{
    m_animateWindow->setRadiusColor( diameter, color );
}

void ClickAnimateControl::setShowTime( double value )
{
    m_showTime = value * 1000;
}

void ClickAnimateControl::setOpacity( double value )
{
    m_animateWindow->setOpacity( value );
}

void ClickAnimateControl::setRadiant( bool value )
{
    m_animateWindow->setRadiant( value );
}
